<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Set {

	public function start() {
		global $cache,$request;

		$action = $request->get( 'p' );

		switch( $action ) {
		case 'editset':
			$this->edit_set();
			break;
		case 'addmenu':
			$this->add_menu();
			break;
		case 'editmenuexe':
			$this->edit_menu_exe();
			break;
		case 'delmenu':
			$this->del_menu();
			break;
		case 'rebuildexe':
			$this->rebuild_exe();
			break;
		default:
			$this->show( $action );
		}
	}

	private function edit_set() {
		global $db,$request,$cache;
		$blog_name = $request->get( 'blogname','P' );
		$description = $request->get( 'blogdescription','P' );
		$key_words = $request->get( 'blogkeywords','P' );
		$beian = $request->get( 'blogbeian','P' );
		$domain = $request->get( 'blogdomain','P' );
		$max_per_page = $request->get( 'blogmaxperpage','P','int' );
		$max_online = $request->get( 'blogmaxonline','P','int' );
		if( $max_online < 5 ) $max_online = 5;
		$filter_words = $request->get( 'blogfilterwords','P' );
		$name_filter = $request->get( 'blognamefilter','P' );
		if( $request->get( 'blogenable','P') == 1 ) {
			$enable = 1;
		}else{
			$enable = 0;
		}
		$close_reason = $request->get( 'blogclosereason','P' );
		if( $request->get( 'blogregenable','P') == 1 ) {
			$reg_enable = 1;
		}else{
			$reg_enable = 0;
		}
		$comment = $request->get( 'blogcomment','P','int' );
		$comment_timer = $request->get( 'blogcomtimer','P','int' );
		$comment_max_length = $request->get( 'blogcommaxlength','P','int' );
		$comment_num = $request->get( 'blogcomnum','P','int' );
		$art_type = $request->get( 'blogarticletype','P' );
		$art_max_length = $request->get( 'blogartmaxlength','P','int' );
		$verify = $request->get( 'verify','P','int' );
		$db->query( "UPDATE `" . DB_PREFIX . "set` SET blogname='$blog_name',description='$description',keywords='$key_words',beian='$beian',domain='$domain',maxperpage=$max_per_page,maxonline=$max_online,filterwords='$filter_words',namefilter= '$name_filter',enable=$enable,closereason='$close_reason',regenable=$reg_enable,comment=$comment,comnum=$comment_num,comtimer=$comment_timer,commaxlength=$comment_max_length,artmaxlength=$art_max_length,arttype='$art_type',verify=$verify" );
		$content = $db->fetch_all( 'SELECT * FROM `' . DB_PREFIX . 'set' );
		$cache->set( 'config', $content[0], 0 );
		echo '<p class="warning">' . L('_SET_EDIT_SUCCESS_') . '</p>';
	}

	private function add_menu() {
		global $db,$request,$cache;
		$name = trim( $request->get( 'name','P' ) );
		$address = trim( $request->get( 'address','P' ) );
		$title = trim( $request->get( 'title','P' ) );
		$type = $request->get( 'type','P','int' );
		$num = $request->get( 'num','P','int' );
		if( $name == '' || $address == '' || $title == '' ) {
			echo '<p class="error">错误：您的输入不完整！</p>';
			return;
		}
		if( $db->result( "SELECT COUNT(mid) FROM `" . DB_PREFIX . "menu` WHERE name='$name'" ) ) {
			echo '<p class="error">错误：导航栏项目名称重复！</p>';
			return;
		}
		$db->query( "INSERT INTO `" . DB_PREFIX . "menu` (`num`,`name`,`title`,`type`,`address`) VALUES ($num,'$name','$title',$type,'$address')" );
		$cache->refresh( 'menu', TRUE );
		echo '<p class="warning">添加导航栏项目成功</p>';
	}

	private function edit_menu_exe() {
		global $db,$cache,$request;
		$id = $request->get( 'id', 'G', 'int');
		$title = trim( $request->get( 'title', 'P') );
		$type = $request->get( 'type', 'P','int');
		$num = $request->get( 'num', 'P','int');
		$address = trim( $request->get( 'address', 'P') );
		if( $address == '' || $title == '' ) {
			echo '<p class="error">错误：您的输入不完整！</p>';
			return;
		}
		$db->query( "UPDATE `" . DB_PREFIX . "menu` SET title='$title', type=$type, num=$num, address='$address' WHERE mid=$id" );
		$cache->refresh( 'menu', TRUE );
		echo '<p class="warning">编辑导航栏项目成功</p>';
	}

	private function del_menu() {
		global $db,$cache,$request;
		$id = $request->get( 'id', 'G', 'int');
		$sql = "DELETE FROM `" . DB_PREFIX . "menu` WHERE mid=$id";
		$db->query( $sql );
		$cache->refresh( 'menu', TRUE );
		echo '<p class="warning">删除导航栏项目成功</p>';
	}

	private function rebuild_exe() {
		global $cache;
		$cache->refresh_all( TRUE );
		echo '<p class="warning">重建系统缓存成功</p>';
	}

	private function show( $action, $message = '', $type = '' ) {
		global $cache;
		if( $message != '' ) {
			$message = '<p class="' . $type . '">' . $message . '</p>';
		}
		@header("content-type: text/html; charset=utf-8");
?>
<div class="admin_panel">
	<?php
	echo $message;
	switch( $action ) {
		case 'menu':
			$this->menu();
			break;
		case 'editmenu':
			$this->edit_menu();
			break;
		case 'rebuild':
			$this->rebuild();
			break;
		case 'config':
			$this->config();
			break;
		default:
			$this->main();
	}
	?>
	<script>
		$(document).ready(function(){
			$('#ajax_form').ajaxForm(function(data){
				showMessage(data);
				$("#admin_loading").css('visibility','hidden');
			});
		});
	</script>
</div>
<?php
	}

	private function main() {
?>
<div class="admin_title"><?php echo L('_BASE_SET_');?></div>
<div class="admin_content">
	<div style="padding:10px;">
		<?php echo L('_UNDEFINED_ACTION_'); ?>
	</div>
</div>
<?php
	}

	private function config() {
		global $cache;
?>
<div class="admin_title"><?php echo L('_BASE_SET_');?></div>
<div class="admin_content">
	<form action="index.php?m=set&p=editset" method="post" id="ajax_form">
		<fieldset>
		<legend> <?php echo L('_BASE_SET_'); ?></legend>
			<div align="left">      
				<table border="0" cellpadding="2" cellspacing="1">
					<tr>
						<td width="180"><div align="right"><?php echo L('_BLOG_NAME_'); ?>&nbsp;&nbsp;</div></td>
						<td align="left"><input name="blogname" type="text" size="30" class="main_text" value="<?php echo $cache->config['blogname']; ?>"/></td>
					</tr>
					<tr>
						<td width="180"><div align="right"><?php echo L('_BLOG_DESCRIPTION_'); ?>&nbsp;&nbsp;</div></td>
						<td align="left"><input name="blogdescription" type="text" class="main_text" value="<?php echo $cache->config['description']; ?>"/></td>
					</tr>
					<tr>
						<td width="180"><div align="right"><?php echo L('_BLOG_KEYWOERDS_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_BLOG_KEYWOERDS_TIPS_'); ?>&nbsp;&nbsp;</div></div></td>
						<td align="left"><input name="blogkeywords" type="text" class="main_text" value="<?php echo $cache->config['keywords']; ?>"/></td>
					</tr>
					<tr>
						<td width="180"><div align="right"><?php echo L('_BLOG_DOMAIN_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_BLOG_DOMAIN_TIPS_'); ?>&nbsp;&nbsp;</div></div></td>
						<td align="left"><input name="blogdomain" type="text" class="main_text" value="<?php echo $cache->config['domain']; ?>"/></td>
					</tr>
					<tr>
						<td width="180"><div align="right"><?php echo L('_BLOG_BEIAN_'); ?>&nbsp;&nbsp;</div></td>
						<td align="left"><input name="blogbeian" type="text" class="main_text" value="<?php echo $cache->config['beian']; ?>"/></td>
					</tr>
					<tr>
						<td width="180"><div align="right"><?php echo L('_BLOG_TONGJI_'); ?>&nbsp;&nbsp;</div></td>
						<td align="left"><input name="blogtongji" type="text" class="main_text" value="<?php echo $cache->config['beian']; ?>"/></td>
					</tr>
					<tr>
						<td width="180"><div align="right"><?php echo L('_MAX_ONLINE_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_MAX_ONLINE_TIPS_'); ?>&nbsp;&nbsp;</div></div></td>
						<td align="left"><input name="blogmaxonline" type="text" class="main_text" value="<?php echo $cache->config['maxonline']; ?>" /></td>
					</tr>
					<tr>
						<td><div align="right"><?php echo L('_BLOG_OPEN_'); ?>&nbsp;&nbsp;</div></td>
						<td align="left"><input name="blogenable" type="checkbox" class="main_checkbox" value="1" <?php if( $cache->config['enable'] ) echo 'checked="checked"'; ?> /></td>
					</tr>
					<tr>
						<td width="180"><div align="right"><?php echo L('_BLOG_CLOSE_REASON_'); ?>&nbsp;&nbsp;</div></td>
						<td align="left"><input name="blogclosereason" type="text" class="main_text" value="<?php echo $cache->config['closereason']; ?>" /></td>
					</tr>
				</table>
			</div>
		</fieldset>
		<fieldset>
		<legend> <?php echo L('_ARTICLE_SET_'); ?></legend>
			<div align="left">
				<table border="0" cellpadding="2" cellspacing="1">
					<tr><td width="180" align="right"><?php echo L('_MAX_PER_PAGE_'); ?>&nbsp;&nbsp;</td><td width="300"><input name="blogmaxperpage" type="text" size="5" class="main_text" value="<?php echo $cache->config['maxperpage'] ?>"/></td></tr>
					<tr><td width="180" align="right">运行模式&nbsp;&nbsp;</td><td width="300"><input type="radio" value="静态" name="blogarticletype" <?php if( $cache->config['arttype'] == "静态" ) echo 'checked="checked"'; ?> />静态 <input type="radio" value="伪静态" name="blogarticletype" <?php if( $cache->config['arttype'] == "伪静态" ) echo 'checked="checked"'; ?> />伪静态 <input type="radio" value="动态" name="blogarticletype" <?php if( $cache->config['arttype'] == "动态" ) echo 'checked="checked"'; ?> />动态</td></tr>
					<tr><td width="180" align="right"><?php echo L('_WORDS_LIMIT_'); ?>&nbsp;&nbsp;</td><td width="300"><input name="blogartmaxlength" type="text" size="5" class="main_text" value="<?php echo $cache->config['artmaxlength'] ?>"/></td></tr>
				</table>
			</div>
		</fieldset>
		<fieldset>
		<legend> <?php echo L('_COMMENT_SET_'); ?></legend>
			<div align="left">
				<table border="0" cellpadding="2" cellspacing="1">
					<tr><td width="180" align="right"><?php echo L('_COMMENT_ENABLE_'); ?>&nbsp;&nbsp;</td><td width="300"><input type="radio" value="0" name="blogcomment" <?php if( $cache->config['comment'] == 0 ) echo 'checked="checked"'; ?> /><?php echo L('_YES_'); ?> <input type="radio" value="1" name="blogcomment" <?php if( $cache->config['comment'] == 1 ) echo 'checked="checked"'; ?> /><?php echo L('_USER_ONLY_'); ?> <input type="radio" value="2" name="blogcomment" <?php if( $cache->config['comment'] == 2 ) echo 'checked="checked"'; ?> /><?php echo L('_NO_'); ?></td></tr>
					<tr><td width="180" align="right"><?php echo L('_TIME_LIMIT_'); ?>&nbsp;&nbsp;</td><td width="300"><input name="blogcomtimer" type="text" size="5" class="main_text" value="<?php echo $cache->config['comtimer'] ?>"/></td></tr>
					<tr><td width="180" align="right"><?php echo L('_WORDS_LIMIT_'); ?>&nbsp;&nbsp;</td><td width="300"><input name="blogcommaxlength" type="text" size="5" class="main_text" value="<?php echo $cache->config['commaxlength'] ?>"/></td></tr>
					<tr><td width="180" align="right"><?php echo L('_COMMENT_PAGE_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_COMMENT_PAGE_TIPS_'); ?>&nbsp;&nbsp;</div></td><td width="300"><input name="blogcomnum" type="text" size="5" class="main_text" value="<?php echo $cache->config['comnum'] ?>"/></td></tr>
				</table>
			</div>
		</fieldset>
		<fieldset>
		<legend> <?php echo L('_USER_SET_'); ?></legend>
			<div align="left">
				<table border="0" cellpadding="2" cellspacing="1">
					<tr><td width="180" align="right"><?php echo L('_REG_ENABLE_'); ?>&nbsp;&nbsp;</td><td width="300"><input name="blogregenable" type="checkbox" value="1" <?php if( $cache->config['regenable'] ) echo 'checked="checked"'; ?> /> </td></tr>
					<tr><td width="180" valign="top"><div align="right"><?php echo L('_USER_FILTER_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_FILTER_TIPS_'); ?>&nbsp;&nbsp;</div></div><td align="left"><textarea name="blognamefilter" cols="50" rows="5"><?php echo $cache->config['namefilter']; ?></textarea></td></td></tr>
				</table>
			</div>
		</fieldset>
		<fieldset>
		<legend> <?php echo L('_SEARCH_SET_'); ?></legend>
			<div align="left">
				<table border="0" cellpadding="2" cellspacing="1">
				</table>
			</div>
		</fieldset>
		<fieldset>
		<legend> <?php echo L('_VERIFY_SET_'); ?></legend>
			<div align="left">
				<table border="0" cellpadding="2" cellspacing="1">
					<tr><td width="180" align="right"><?php echo L('_VERIFY_ENABLE_'); ?>&nbsp;&nbsp;</td><td width="300"><input type="radio" value="0" name="verify" <?php if( $cache->config['verify'] == 0 ) echo 'checked="checked"'; ?> /><?php echo L('_YES_'); ?> <input type="radio" value="1" name="verify" <?php if( $cache->config['verify'] == 1 ) echo 'checked="checked"'; ?> /><?php echo L('_GUEST_ONLY_'); ?> <input type="radio" value="2" name="verify" <?php if( $cache->config['verify'] == 2 ) echo 'checked="checked"'; ?> /><?php echo L('_NO_'); ?></td></tr>
				</table>
			</div>
		</fieldset>
		<fieldset>
		<legend> <?php echo L('_FILTER_SET_'); ?></legend>
			<div align="left">
				<table border="0" cellpadding="2" cellspacing="1">
					<tr><td width="180" valign="top"><div align="right"><?php echo L('_FILTER_WORDS_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_FILTER_TIPS_'); ?>&nbsp;&nbsp;</div></div><td align="left"><textarea name="blogfilterwords" cols="50" rows="5"><?php echo $cache->config['filterwords'] ?></textarea></td></td></tr>
				</table>
			</div>
		</fieldset>
		<div class="main_button"><input type="submit" onclick="$('#admin_loading').css('visibility','visible');" class="main_button" value=" <?php echo L('_SAVE_'); ?> " /></div>
	</form>
</div>
<?php
	}

	private function menu() {
		global $db;
		$sql = 'SELECT * FROM `' . DB_PREFIX . 'menu` ORDER BY num ASC,mid DESC';
		$menu_array = $db->fetch_all( $sql );
?>
<div class="admin_title"><?php echo L('_MENU_SET_'); ?></div>
<div class="admin_content">
	<div style="padding:5px;margin:2px;">
		<table style="table-layout: fixed;word-wrap: break-word;" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#bbbbbb">
			<tr align="center" height="22" bgcolor="#ffffff">
				<td width="3%"></td>
				<td width="4%"><?php echo L('_NUM_'); ?></td>
				<td width="10%"><?php echo L('_NAME_'); ?></td>
				<td width="10%"><?php echo L('_TITLE_'); ?></td>
				<td width="7%"><?php echo L('_SORT_'); ?></td>
				<td width="10%"><?php echo L('_TYPE_'); ?></td>
				<td width="43%"><?php echo L('_ADDRESS_'); ?></td>
				<td width="13%"><?php echo L('_OPERATION_'); ?></td>
			</tr>
			<?php
			$i = 1;
			foreach( $menu_array as $value ) {
			?>
			<tr align="center" height="20" bgcolor="#ffffff">
				<td ></td>
				<td><?php echo $i ?></td>
				<td><?php echo $value["name"] ?></td>
				<td><?php echo $value["title"] ?></td>
				<td><?php echo $value["num"] ?></td>
				<?php
				If( $value["type"] == 0 ) {
					$menu_type = L('_INNER_ADDRESS_');
				}else{
					$menu_type = L('_OUTER_ADDRESS_');
				}
				?>
				<td><?php echo $menu_type ?></td>
				<td><?php echo $value["address"] ?></td>
				<td>
					<span class="Control">
					<a href="#" onclick="ajax_load('<?php echo ADMIN_PATH ?>','set','editmenu',null,'<?php echo $value['mid'] ?>');return false;"><?php echo L('_EDIT_'); ?></a>&nbsp; &nbsp;
					<a href="#" onclick="if(confirm('<?php echo L('_DELETE_CONFIRM_1_'); ?>'))ajax_get('<?php echo ADMIN_PATH ?>','set','delmenu',null,'<?php echo $value['mid'] ?>');return false;"><?php echo L('_DELETE_'); ?></a>
					</span>
				</td>
			</tr>
			<?php
				$i ++;
			}
			?>
		</table>
	</div>
	<form name="smslist" action="index.php?m=set&p=addmenu" method="post" id="ajax_form">
	<fieldset>
	<legend> <?php echo L('_MENU_ADD_'); ?></legend>      
		<table border="0" cellpadding="2" cellspacing="1">
			<tr>
				<td width="180"><div align="right"><?php echo L('_NAME_'); ?>&nbsp;&nbsp;</div></td>
				<td align="left"><input name="name" type="text" size="30" maxlength="50" class="main_text" /></td>
			</tr>
			<tr>
				<td width="180"><div align="right"><?php echo L('_TITLE_'); ?>&nbsp;&nbsp;</div></td>
				<td align="left"><input name="title" type="text" size="30" maxlength="100" class="main_text" /></td>
			</tr>
			<tr>
				<td width="180"><div align="right"><?php echo L('_SORT_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_SORT_TIPS_'); ?>&nbsp;&nbsp;</div></div></td>
				<td align="left"><input name="num" type="text" size="30" maxlength="5" class="main_text" /></td>
			</tr>
			<tr>
				<td width="180"><div align="right"><?php echo L('_ADDRESS_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_MENU_ADDRESS_TIPS_'); ?>&nbsp;&nbsp;</div></div></td>
				<td align="left"><input name="address" type="text" size="30" maxlength="100" class="main_text" /></td>
			</tr>
			<tr>
				<td width="180"><div align="right"><?php echo L('_TYPE_'); ?>&nbsp;&nbsp;</div></td>
				<td align="left"><input type="radio" value="0" name="type" checked="checked" /><?php echo L('_INNER_ADDRESS_'); ?> <input type="radio" value="1" name="type" /><?php echo L('_OUTER_ADDRESS_'); ?></td>
			</tr>
			<tr>
				<td width="180"><div align="right"></div></td>
				<td align="left"><input type="submit" class="main_button" onclick="$('#admin_loading').css('visibility','visible');" value=" <?php echo L('_ADD_'); ?> " /></td>
			</tr>
		</table>
	</fieldset>
	</form>
	<div class="main_button"></div>
</div>
<?php
	}

	private function edit_menu() {
		global $db,$request;
		$id = $request->id;
		$sql = "SELECT * FROM " . DB_PREFIX . "menu WHERE mid=$id";
		$menu_item = $db->fetch_one_array( $sql );
?>
<div class="admin_title"><?php echo L('_BASE_SET_');?></div>
<div class="admin_content">
	<fieldset>
	<legend> <?php echo L('_MENU_EDIT_'); ?></legend>
		<div align="left">
		<form name="smslist" action="index.php?m=set&p=editmenuexe&id=<?php echo $id ?>" method="post" id="ajax_form">
		<table border="0" cellpadding="2" cellspacing="1">
			<tr>
				<td width="180"><div align="right"><?php echo L('_NAME_'); ?>&nbsp;&nbsp;</div></td>
				<td align="left"><?php echo $menu_item ['name'] ?></td>
			</tr>
			<tr>
				<td width="180"><div align="right"><?php echo L('_TITLE_'); ?>&nbsp;&nbsp;</div></td>
				<td align="left"><input name="title" type="text" size="30" maxlength="100" class="main_text" value="<?php echo $menu_item ['title'] ?>" /></td>
			</tr>
			<tr>
				<td width="180"><div align="right"><?php echo L('_SORT_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_SORT_TIPS_'); ?>&nbsp;&nbsp;</div></div></td>
				<td align="left"><input name="num" type="text" size="30" maxlength="5" class="main_text" value="<?php echo $menu_item ['num'] ?>" /></td>
			</tr>
			<tr>
				<td width="180"><div align="right"><?php echo L('_ADDRESS_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_MENU_ADDRESS_TIPS_'); ?>&nbsp;&nbsp;</div></div></td>
				<td align="left"><input name="address" type="text" size="30" maxlength="100" class="main_text" value="<?php echo $menu_item ['address'] ?>" /></td>
			</tr>
			<tr>
				<td width="180"><div align="right"><?php echo L('_TYPE_'); ?>&nbsp;&nbsp;</div></td>
				<td align="left"><input type="radio" value="0" name="type" <?php if( $menu_item ['type'] == 0 ) echo 'checked="checked"'; ?> /><?php echo L('_INNER_ADDRESS_'); ?> <input type="radio" value="1" name="type" <?php if( $menu_item ['type'] == 1 ) echo 'checked="checked"'; ?> /><?php echo L('_OUTER_ADDRESS_'); ?></td>
			</tr>
			<tr>
				<td width="180"><div align="right"></div></td>
				<td align="left"><input type="submit" class="main_button" onclick="$('#admin_loading').css('visibility','visible');" value=" <?php echo L('_EDIT_'); ?> " /></td>
			</tr>
		</table>
		</form>
	</fieldset>
	<div class="main_button"></div>
</div>
<?php
	}

	private function rebuild() {
?>
<div class="admin_title"><?php echo L('_REBUILD_CACHE_'); ?></div>
<div class="admin_content">
	<div style="padding:10px;">
		<a href="#" onclick="ajax_get('<?php echo ADMIN_PATH ?>','set','rebuildexe');return false;"><?php echo L('_REBUILD_CACHE_'); ?></a>
	</div>
</div>
<?php
	}
}
?>
